﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Text;
using Portal.Environment;
using Portal.Language;
using Portal.Interface;

namespace Portal.CMS
{
    public class AlbumItems : WebPart
    {
        public override string WebPartId
        {
            get
            {
                return "AlbumItems";
            }
        }

        public string GetViewAlbum(string belongSite, string Subject, string ShortId)
        {
            Subject = Environments.ConvertSubject(Subject);
            string Url = Environments.HttpRoot + "/" + Environments.AppendAlias + "-" + belongSite + "/" + Subject + "-va-" + ShortId + "." + Environments.Extension;//?NewsId=" + ShortId;
            if (Environments.MultiLanguageInSite == 1)
            {
                Url = Url + "?lang=" + Environments.ActiveLanguage;
            }
            return Url;
        }


        [AjaxPro.AjaxMethod]
        public string GetHtml(string ActiveLanguage, string AlbumId)
        {
            try
            {
                string RetHtml =
                                "<div id=divProcessing></div>";
                DataTable dtAlbumItem = new AlbumBLL().ReadingAlbumItems(AlbumId);
                if (dtAlbumItem.Rows.Count == 0)
                {
                    RetHtml += "<div class=\"cssNoData\">" + ResourceBLL.GetText(WebPartId, "NoData", "Chưa có dữ liệu nào trong chuyên mục này!") + "</div>";
                }
                else
                {
                    RetHtml +=
                               "<table>\r\n" +
                               "   <tr>\r\n" +
                               "       <th>" + ResourceBLL.GetText(WebPartId, "GroupName", "Tên nhóm") + "</th>\r\n" +
                               "       <th>" + ResourceBLL.GetText(WebPartId, "IndexOrder", "Vị trí") + "</th>\r\n" +
                               "       <th>" + ResourceBLL.GetText(WebPartId, "Clicked", "Số lần xem") + "</th>\r\n" +
                        //"       <th>" + ResourceBLL.GetText(WebPartId, "MaxNews", "Số tin") + " </th>\r\n" +
                        //"       <th></th>\r\n" +
                               "       <th>" + ResourceBLL.GetText(WebPartId, "Delete", "Xóa") + "</th>\r\n" +
                        //Add new by dvtien for cdspna project on 23/02/2012
                               "       <th>" + ResourceBLL.GetText(WebPartId, "SetDefault", "Hiển thị trang chủ") + "</th>\r\n" +
                               "   </tr>";

                    for (int iIndex = 0; iIndex < dtAlbumItem.Rows.Count; iIndex++)
                    {
                        string AlbumItemId = (string)dtAlbumItem.Rows[iIndex]["AlbumItemId"];
                        string Subject = "";
                        try
                        {
                            Subject = (string)dtAlbumItem.Rows[iIndex][ActiveLanguage + "Subject"];
                        }
                        catch { }
                        int IndexOrder = (int)dtAlbumItem.Rows[iIndex]["IndexOrder"];
                        int Clicked = 0;
                        try
                        {
                            Clicked = (int)dtAlbumItem.Rows[iIndex]["Clicked"];
                        }
                        catch { }

                        string ShortId = (string)dtAlbumItem.Rows[iIndex]["ShortId"];
                        string belongSite = (string)dtAlbumItem.Rows[iIndex]["belongSite"];
                        string LinkText = GetViewAlbum(belongSite, Subject, ShortId);
                        LinkText = LinkText.Substring(LinkText.LastIndexOf("/") + 1);
                        LinkText = "<a target=\"_blank\" href=\"" + Environments.HttpRoot + "/" + Environments.AppendAlias + "-" + belongSite + "/" + LinkText + "\">" + LinkText + "</a>";
                        string IsChecked = "";
                        try
                        {
                            if ((bool)dtAlbumItem.Rows[iIndex]["setDefaulView"])
                            {
                                IsChecked = "checked";
                            }
                        }
                        catch {
                            IsChecked = "";
                        }

                        string DeleteUrl = "javascript:DeleteAlbumItem('" + AlbumId + "','" + AlbumItemId + "');";                       
                        RetHtml += "<tr>\r\n" +
                                   "    <td class=cssCol1><input type=hidden id=txtId_" + iIndex + " value=\"" + AlbumItemId + "\"><input id=txtSubject_" + iIndex + " class=cssSubjectAlbumItem value=\"" + Subject + "\"><br><span class=cssViewItemType>" + LinkText + "</span></td>\r\n" +
                                   "    <td class=cssCol2><input id=txtIndexOrder_" + iIndex + " class=cssIndexOrder value=\"" + IndexOrder + "\"></td>\r\n" +
                                   "    <td class=cssCol5>" + Clicked.ToString("#,##0") + "</td>\r\n" +
                                   "    <td class=cssCol4><a href=\"" + DeleteUrl + "\">" + ResourceBLL.GetText(WebPartId, "Delete", "Xóa") + "</a></td>\r\n" +
                            //Add new by dvtien for cdspna project on 23/02/2012
                                   "    <td class=cssCol4 align = center><input type = \"radio\" name = \"rdo_setdefault\" " + IsChecked + " value=\"" + AlbumItemId + "\" id = rdoId_" + iIndex + "></td>\r\n" +
                                   "</tr>\r\n";
                    }
                    RetHtml += "</table>\r\n";
                    if (dtAlbumItem.Rows.Count == 0)
                    {
                        RetHtml += "<div class=cssRecordNotFound>" + ResourceBLL.GetText(WebPartId, "RecordNotFound", "Không tìm thấy bản ghi!") + "</div>\r\n";
                    }
                    RetHtml += "<input type=hidden id=txtMaxItems value=\"" + dtAlbumItem.Rows.Count + "\">";
                }
                return RetHtml;
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }

        public override string RenderWebPart(LoadParamCls loadParamCls)
        {
            string AlbumId = (string)System.Web.HttpContext.Current.Request["AlbumId"];

            string SelectLanguageText =
                "<select id=divSelectLanguage onchange=javascript:DetectHtmlBody('" + AlbumId + "');>";
            string[] Languages = Environment.Environments.GetLanguages();
            for (int iIndex = 0; iIndex < Languages.Length; iIndex++)
            {
                if (Environment.Environments.ActiveLanguage.Equals(Languages[iIndex]))
                {
                    SelectLanguageText += "<option selected value=\"" + Languages[iIndex] + "\">" + Languages[iIndex] + "</option>";
                }
                else
                {
                    SelectLanguageText += "<option value=\"" + Languages[iIndex] + "\">" + Languages[iIndex] + "</option>";
                }
            }
            SelectLanguageText += "</select>";

            AjaxPro.Utility.RegisterTypeForAjax(typeof(AlbumItems));
            string RetHtml =
                WebPartUtilities.AttachJavascript("Portal.CMS", "Album.js");

            string AlbumItemAddRootUrl = "javascript:ClientShowAddAlbumItemRootForm('" + AlbumId + "');";
            RetHtml +=

                //"<div class=cssForm>" +
                ///"<div class=cssFormTitle>" + ResourceBLL.GetText(WebPartId,"AlbumItemHeader","Tin bài theo nhóm") + "</div>"+
                Environment.Environments.GetFormDialog(
                "<div class=cssAlbumItemAddRoot>" + SelectLanguageText + "<input type=button onclick=\"" + AlbumItemAddRootUrl + "\" value=\"" + ResourceBLL.GetText(WebPartId, "AlbumItemAddRoot", "Thêm mới") + "\">&nbsp;<input type=button onclick=\"javascript:ClientSaveAlbumItems('" + AlbumId + "');\" value=\"" + ResourceBLL.GetText(WebPartId, "Save", "Ghi lại") + "\">&nbsp;<input type=button onclick=\"javascript:ReadingAlbumItem('" + AlbumId + "');\" value=\"" + ResourceBLL.GetText(WebPartId, "RefreshButton", "Đọc lại") + "\">&nbsp;<input type=button onclick=\"javascript:window.close();\" value=\"" + ResourceBLL.GetText(WebPartId, "CloseButton", "Đóng") + "\"></div>" +
                "<div id=divContentData>" + GetHtml(Environments.ActiveLanguage, AlbumId) + "</div>", ResourceBLL.GetText(WebPartId, "AlbumItemHeader", "Tin bài theo nhóm"));


            return RetHtml;
        }

        [AjaxPro.AjaxMethod]
        public string GetAddAlbumItemFormUrl(string AlbumId)
        {
            return Screen.BuildDialogUrl(new ParamCls[]{
                new ParamCls("WebPartId",new AddAlbumItem().WebPartId),
                new ParamCls("AlbumId",AlbumId)
            });
        }

        [AjaxPro.AjaxMethod]
        public string GetAddSubAlbumItemFormUrl(string AlbumId, string ParentAlbumItemId)
        {
            return Screen.BuildDialogUrl(new ParamCls[]{
                new ParamCls("WebPartId",new AddAlbumItem().WebPartId),
                new ParamCls("AlbumId",AlbumId),
                new ParamCls("ParentAlbumItemId",ParentAlbumItemId),
            });
        }

        [AjaxPro.AjaxMethod]
        public string GetConfirmMsg()
        {
            return ResourceBLL.GetText(WebPartId, "ConfirmDeleteMsg", "Bạn đã chắc chắn xóa chưa?");
        }

        [AjaxPro.AjaxMethod]
        public string ServerSideDeleteAlbumItem(string AlbumItemId)
        {
            try
            {
                new AlbumBLL().DeleteAlbumItem(null, AlbumItemId);
                return ResourceBLL.GetText(WebPartId, "DeleteOkMessage", "Xóa thành công!");
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }


        [AjaxPro.AjaxMethod]
        public string ServerSideSaveAlbumItem(string AlbumId, string ActiveLanguage, string AlbumItemId, string Subject, int IndexOrder, bool isCheck)
        {
            try
            {
                new AlbumBLL().
                    SaveAlbumItem(null, ActiveLanguage, AlbumItemId, Subject, IndexOrder);
                if (isCheck)
                {
                    new AlbumBLL().SetDefaultView(
                    null, AlbumItemId, AlbumId);
                }
                return "";
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }


        [AjaxPro.AjaxMethod]
        public string ServerSideGetSaveOkMsg()
        {
            return ResourceBLL.GetText(WebPartId, "MessageUpdateOk", "Cập nhật thành công!");
        }

        [AjaxPro.AjaxMethod]
        public string GetConfirmPublishingMsg()
        {
            return ResourceBLL.GetText(WebPartId, "ConfirmPublishingMsg", "Bạn đã chắc chắn phát hành lên website public chưa?");
        }

        [AjaxPro.AjaxMethod]
        public string ServerSideItemPublishing(string AlbumItemId)
        {
            try
            {
                Environment.Environments.CheckSessionTimeOut(true);
                //new AlbumBLL().ItemPublishingPublicWebSite(AlbumItemId);
                return ResourceBLL.GetText(WebPartId, "PublishingOk", "Xuất bản lên website thành công!");
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }

        [AjaxPro.AjaxMethod]
        public string GetProcessingText()
        {
            return ResourceBLL.GetText(WebPartId, "ProcessingText", "Đang xử lý dữ liệu ...");
        }

    }
}
